package com.example.seh.person;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.seh.R;

import java.util.ArrayList;
import java.util.List;

public class AddressChooseActivity extends AppCompatActivity implements View.OnClickListener{
    private Button park1, park2, park3, unit1, unit2, unit3, unit4, unit5, unit6, unit7, unit8, unit9;
    private Button submit;
    private String parkStr, unitStr, newAddress;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_choose);
        getViews();
        setListeners();
    }

    private void setListeners() {
        park1.setOnClickListener(this);
        park2.setOnClickListener(this);
        park3.setOnClickListener(this);
        unit1.setOnClickListener(this);
        unit2.setOnClickListener(this);
        unit3.setOnClickListener(this);
        unit4.setOnClickListener(this);
        unit5.setOnClickListener(this);
        unit6.setOnClickListener(this);
        unit7.setOnClickListener(this);
        unit8.setOnClickListener(this);
        unit9.setOnClickListener(this);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (parkStr != null && unitStr != null) {
                    newAddress = parkStr + unitStr;
                    Log.e("msg", newAddress);
                    Intent intent = getIntent();
                    intent.putExtra("address", newAddress);
                    setResult(101, intent);
                    finish();
                } else {
                    Toast.makeText(getApplicationContext(), "请选择完整", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.cpy:
                setParkEnable(park1);
                break;
            case R.id.qzy:
                setParkEnable(park2);
                break;
            case R.id.csy:
                setParkEnable(park3);
                break;
            case R.id.one:
                setUnitEnable(unit1);
                break;
            case R.id.two:
                setUnitEnable(unit2);
                break;
            case R.id.three:
                setUnitEnable(unit3);
                break;
            case R.id.four:
                setUnitEnable(unit4);
                break;
            case R.id.five:
                setUnitEnable(unit5);
                break;
            case R.id.six:
                setUnitEnable(unit6);
                break;
            case R.id.seven:
                setUnitEnable(unit7);
                break;
            case R.id.eight:
                setUnitEnable(unit8);
                break;
            case R.id.nine:
                setUnitEnable(unit9);
                break;
        }
    }
    private void setParkEnable(Button btn) {
        List<Button> buttonList = new ArrayList<>();
        if (buttonList.size()==0){
            buttonList.add(park1);
            buttonList.add(park2);
            buttonList.add(park3);
        }
        for (int i = 0; i <buttonList.size() ; i++) {
            buttonList.get(i).setEnabled(true);
        }
        btn.setEnabled(false);
        parkStr = btn.getText().toString();
    }
    private void setUnitEnable(Button btn) {
        List<Button> buttonList = new ArrayList<>();
        if (buttonList.size()==0){
            buttonList.add(unit1);
            buttonList.add(unit2);
            buttonList.add(unit3);
            buttonList.add(unit4);
            buttonList.add(unit5);
            buttonList.add(unit6);
            buttonList.add(unit7);
            buttonList.add(unit8);
            buttonList.add(unit9);
        }
        for (int i = 0; i <buttonList.size() ; i++) {
            buttonList.get(i).setEnabled(true);
        }
        btn.setEnabled(false);
        unitStr = btn.getText().toString();
    }
    private void getViews() {
        park1 = findViewById(R.id.cpy);
        park2 = findViewById(R.id.qzy);
        park3 = findViewById(R.id.csy);
        unit1 = findViewById(R.id.one);
        unit2 = findViewById(R.id.two);
        unit3 = findViewById(R.id.three);
        unit4 = findViewById(R.id.four);
        unit5 = findViewById(R.id.five);
        unit6 = findViewById(R.id.six);
        unit7 = findViewById(R.id.seven);
        unit8 = findViewById(R.id.eight);
        unit9 = findViewById(R.id.nine);
        submit = findViewById(R.id.submit);
    }
}